define(function(require){
    var main = require('/static/main.js');
    var m = new main();
    function Cont(){
        this.Fun._init();
    }
    Cont.prototype.Fun = {
        _init:function(){
            var _this = this;
            this.getdetial();
            this.bindEvent();
        },
        getdetial:function(){
            var _this = this;
            $.ajax({
                type: "POST",
                url: "/site/get-notice-type",
                dataType: "json",
                success: function (ret) {
                    if(ret.IsBizSuccess){
                        var navtab = '';
                        var panel = '';
                       $.each( ret.Models,function(k,v){
                            navtab += '<li  class="'+ (k == 0 ? 'active' : '')+'">\
                            <a href="#table'+k+'" data-toggle="tab" aria-expanded="false" data-type="100" style="border-left:'+ (k == 0 ? 'none' : '')+'">\
                            <span class="hidden-xs">'+ v.Text+'</span>\
                            </a>\
                            </li>'
                            panel+='<div id="table'+k+'" class="tab-pane '+ (k == 0 ? 'active' : '')+' ">\
                            <div class="col-md-12 col-sm-12 col-xs-12">\
                            <table id="datatable'+k+'" class="table table-striped table-bordered">\
                                <thead>\
                                    <tr>\
                                        <th class="">消息内容</th>\
                                        <th class="widht">时间</th>\
                                    </tr>\
                                </thead>\
                                <tbody>\
                                </tbody>\
                            </table>\
                            </div>\
                            </div>';
                        });
                        $(".nav-tabs").html(navtab);
                        $(".tab-content").html(panel);
                        $.each( ret.Models,function(k,v){
                            _this.settable(k, v.Value);
                        });
                        $(".panel-default").fadeIn();
                    }else{

                    }
                }
            });
        },
        settable:function(k,type){
            var table1 = $('#datatable'+k).dataTable({
                'ordering':false,
                "serverSide": true,
                "searching":false,
                'bLengthChange': false,
                'bPaginate': true,
                "bDestory": true,
                "bRetrieve": true,
                "bFilter":false,
                "bSort": false,
                "bProcessing": true,
                "autoWidth": false,
                ajax: function (data, callback, settings) {
                    var param = {};
                    //封装请求参数
                    param.PageSize = data.length;//页面显示记录条数，在页面显示每页显示多少项的时候
                    param.start = data.start;//开始的记录序号
                    param.PageNo = (data.start / data.length)+1;//当前页码
                    param.NoticeType = type;
                    //ajax请求数据
                    $.ajax({
                        type: "POST",
                        url: "/site/get-notice-list",
                        cache: false,  //禁用缓存
                        data: param,  //传入组装的参数
                        dataType: "json",
                        success: function (result) {
                            var returnData = {};
                            returnData.draw = data.draw;
                            returnData.recordsTotal = result.total;
                            returnData.recordsFiltered = result.total;
                            returnData.data = result.data;
                            callback(returnData);
                        }
                    });
                },
                "aoColumns":[
                    {"mDataProp": 'Content',
                     "sClass": "oneoverflow",
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        if(oData.IsRead){
                            var str  = '<i class="fa fa-circle m-r-5" style="color: #6e8cd7;font-size: 10px;opacity: 0;"></i>'+oData.Content;
                        }else{
                            var str  = '<i class="fa fa-circle readtrue m-r-5" data-id="'+oData.NoticeId+'" style="color: #999;font-size: 10px;vertical-align: 2px;"></i>'+oData.Content;
                        }
                        $(nTd).html(str);

                    }
                    },
                    {
                        "mDataProp": 'CreateTime',
                        render:function(data){
                            var time = m.formatTimeAll2(data);
                            return time;
                        }
                    }
                ],
            }).api();
        },
        bindEvent:function(){
            // 删除
            var delactive = false;
            $(document).on('click','.DelBtn',function(){
                if(delactive == true){
                    return false;
                }
                var id = $(this).attr('data-id');
                delactive = true;
                var index = $(this).closest(".tab-pane").index();
                $.ajax({
                    type: "POST",
                    url: "/site/notice-delete",
                    data :{NoticeId:id},
                    dataType: "json",
                    success: function (ret){
                        delactive = false;
                        if(ret.IsBizSuccess){
                            layer.msg("删除成功");
                            var table2 = $('#datatable'+index).DataTable();
                            table2.ajax.reload();
                        }else{
                            layer.msg("删除失败失败");
                        }
                    }
                });
            })
            // 消息置为已读
            var delactive = false;
            $(document).on('click','.table tr',function(){
                if(delactive == true && $(this).find(".readtrue").length > 0){
                    return false;
                }
                var id = $(this).find(".readtrue").attr('data-id');
                //delactive = true;
                var obj = $(this);
                $.ajax({
                    type: "POST",
                    url: "/site/notice-read",
                    data :{NoticeId:id},
                    dataType: "json",
                    success: function (ret){
                        delactive = false;
                        if(ret.IsBizSuccess){
                            obj.find(".readtrue").removeClass(".readtrue").css('opacity',0).attr('data-id','');
                        }else{
                            layer.msg(ret.ErrMsg);
                        }
                    }
                });
            })
        }

    }
    var a = new Cont();

})